/**
 * Created by 121 on 2016/4/08.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'template',
    'collection',
    'model',
    'dialog',
    'calendar',
    'highCharts'
], function ($, common, ready, template, Collection, Model, dialog, calendar, highCharts) {
    var index = function () {
        this.init();
    };
    //$('.sidebar').css({'display': 'none'});
    //$('header').css({'display': 'none'});
    //$('.main').css({'margin': '50px 50px'});
    index.prototype = {
        init: function () {
            this.event();
            //this.fetch();
        },
        event: function () {
            var _this = this;
            $('.form_datetime').calendar();
            $.ajax({
                url: _global.url.api + 'data/ah_gd_curve_info',
                type: 'get',
                success: function (res) {
                    for (var i = 0; i < res.data.length; i++) {
                        $('.checkbox-list').append(template('checkbox-item', res.data[i]));
                    }
                    ////alert($('input[name="curve-list"]').eq(0).val());
                    //$('input[name="curve-list"]').eq(0).attr('checked');
                }
            });
            var success = function (id, type, start_time, end_time) {
                $.ajax({
                    url: _global.url.api + 'data/ah_gd_curve',
                    type: 'get',
                    data: {id: id, type: type, start_time: start_time, end_time: end_time},
                    success: function (res) {
                        $('#gd_curve').highcharts({
                            chart: {
                                zoomType: 'xy'
                            },
                            title: {
                                text: ''
                            },
                            xAxis: [{
                                //tickInterval: res.data.tickInterval,
                                //showFirstLabel: res.data.showFirstLabel,
                                categories: res.data.tab,
                                title: {
                                    text: res.data.title_x,
                                    style: {
                                        color: '#89A54E'
                                    }
                                },
                                labels: {
                                    //rotation: -45,
                                    align: 'right',
                                    style: {
                                        fontSize: '10px'
                                    }
                                }
                            }],
                            yAxis: [{ // Primary yAxis
                                allowDecimals: false,
                                floor: 0,
                                labels: {
                                    format: '{value}',
                                    style: {
                                        color: '#89A54E'
                                    }
                                },
                                title: {
                                    text: '',
                                    style: {
                                        color: '#89A54E'
                                    }
                                }
                            }],
                            tooltip: {
                                shared: true
                            },
                            legend: {
                                layout: 'vertical',
                                align: 'right',
                                verticalAlign: 'middle',
                                backgroundColor: '#FFFFFF'
                            },
                            series: res.data.series

                        });
                    }
                });
            };
            success();
            $('#btn-search').on('click', function (e) {
                e.preventDefault();
                var type = $('input[name="type"]:checked').val();
                var start_time = $('.start-time').val();
                var end_time = $('.end-time').val();
                var id = new Array();
                $("input[name='curve-list']:checked").each(function () {
                    id.push($(this).val());
                });
                id = id.join(',');
                console.log(type, start_time, end_time, id)
                success(id, type, start_time, end_time);
            });
            //to excel
            var href;
            var toExcelWindow;
            var data;
            var total;
            $(".btn-excel").on("click", function (e) {
                e.preventDefault();
                href = $(this).attr('href');
                toExcelWindow = dialog({
                    content: '请稍等 ,正在导出...',
                    width: 200,
                    modal: true
                }).show();
                toExcel();
            });
            var toExcel = function () {
                var start_time = $('.start-time').val();
                var end_time = $('.end-time').val();
                $.ajax({
                    url: _global.url.api + 'data/ah_gd_curve_list',
                    type: 'get',
                    data: {start_time: start_time, end_time: end_time},
                    success: function (res) {
                        data = res.data;
                        total = res.count;
                        toExcelWindow.close();
                        var limit = 1000;
                        if (total < 1) {
                            alert('没有需要导出的订单');
                            return false;
                        }

                        href = href + '?&limit=' + limit + '&end_time=' + end_time + '&start_time=' + start_time + '&offset=';

                        if (total > limit) {
                            var showData = {};
                            showData.pages = Math.ceil(total / limit);
                            showData.total = total;
                            showData.limit = limit;
                            showData.href = href;
                            dialog({
                                title: '导出订单列表Excel文件',
                                content: template('to-excel-tpl', showData),
                                width: 600,
                                modal: true,
                                ok: function () {
                                },
                                cancel: function () {
                                }
                            }).show();
                        } else {
                            window.location.href = href;
                        }
                    }
                });
            }
        }
    };
    new index();
});